package com.example.framework509.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @desc http请求工具类
 */
@Component
public class RestTemplateUtil {

    public JSONObject getRequest(String address,Map<String,Object> params){
        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(address);
        if (params!=null&&params.size()!=0){
            for (String key: params.keySet()){
                uriBuilder = uriBuilder.queryParam(key, params.get(key));
            }
        }
        URI uri = uriBuilder.build().encode().toUri();
        RestTemplate restTemplate=new RestTemplate();
        String data=restTemplate.getForObject(uri,String.class);
        JSONObject jsonObject = JSONObject.parseObject(data);
        return jsonObject;
    }

    public JSONObject postRequest(String address,Map<String,Object> params){
        MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
        String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

        JSONObject json = new JSONObject();
        if (params!=null&&params.size()!=0){
            for (String key: params.keySet()){
                json.put(key, params.get(key));
            }
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(APPLICATION_JSON_UTF8);
        headers.add("Accept", APPLICATION_JSON_UTF8_VALUE);
        HttpEntity<JSONObject> entity = new HttpEntity<>(json, headers);

        String data = new RestTemplate().postForObject(address, entity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(data);
        return jsonObject;
    }

    public JSONObject postRequestByJsonPara(String address,JSONObject params){
        MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
        String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(APPLICATION_JSON_UTF8);
        headers.add("Accept", APPLICATION_JSON_UTF8_VALUE);
        HttpEntity<JSONObject> entity = new HttpEntity<>(params, headers);

        String data = new RestTemplate().postForObject(address, entity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(data);
        return jsonObject;
    }

    public JSONObject postRequestFile(String address,Map<String,Object> params){
        MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
        String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

        MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
        if (params!=null&&params.size()!=0){
            for (String key: params.keySet()){
                map.add(key,params.get(key));
            }
        }
        //map.add("articleId","45713ff41a5944fcb1e54b0bdd1647fb");

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add("Accept", APPLICATION_JSON_UTF8_VALUE);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(map, headers);

        RestTemplate restTemplate=new RestTemplate();
        String data = new RestTemplate().postForObject(address, entity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(data);
        return jsonObject;
    }

    public JSONObject postRequestMerge(String address,Map<String,Object> params){
        MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
        String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

        MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
        if (params!=null&&params.size()!=0){
            for (String key: params.keySet()){
                map.add(key, params.get(key));
            }
        }
        map.add("userId","c439d26a7297407581e6ee267dd5196c");

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add("Accept", APPLICATION_JSON_UTF8_VALUE);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(map, headers);

        RestTemplate restTemplate=new RestTemplate();
        String data = new RestTemplate().postForObject(address, entity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(data);
        return jsonObject;
    }

    public JSONObject postRequestAddPakToProj(String address,
                                              String projId,
                                              String scenesId,
                                              String scenesName,
                                              String pakName,
                                              String callback,
                                              Boolean isBuildDbIndex){
        MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
        String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";


        JSONObject jsonObjectPak = new JSONObject();
        jsonObjectPak.put("name",pakName);

        JSONArray jsonArrayPak = new JSONArray();
        jsonArrayPak.set(0,jsonObjectPak);

        JSONArray jsonArray = new JSONArray();

        JSONObject jsonObjectSce = new JSONObject();
        jsonObjectSce.put("sceneId",scenesId);
        jsonObjectSce.put("sceneName",scenesName);
        jsonObjectSce.put("paks",jsonArrayPak);

        jsonArray.set(0,jsonObjectSce);

        JSONObject json = new JSONObject();
        json.put("projId",projId);
        json.put("scenes",jsonArray);
        json.put("execConvertingCallback",callback);
        json.put("isBuildDbIndex",isBuildDbIndex);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(APPLICATION_JSON_UTF8);
        headers.add("Accept", APPLICATION_JSON_UTF8_VALUE);
        HttpEntity<JSONObject> entity = new HttpEntity<>(json, headers);

        RestTemplate restTemplate=new RestTemplate();
        String data = new RestTemplate().postForObject(address, entity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(data);
        return jsonObject;
    }
}